﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverChart.Configuration;
using SilverChart.Components;
using System.Collections.Generic;
using SilverChart.Core.Log;

namespace SilverChart {
    public abstract class CartesianCoordinateChart : ChartBase, RelativeClientConsumer {
        private static Logger logger = Logger.GetLogger<CartesianCoordinateChart>();

        private CartesianCoordinateAxis xAxis;
        private CartesianCoordinateAxis yAxis;

        public CartesianCoordinateChart(Element data)
            : base(data) {
            CreateDependences();
        }


        public virtual CartesianCoordinateAxis XAxis {
            get {
                if (xAxis == null) {
                    IList<XAxisComponent> axixs = WidgetsManager.GetCompatibleComponents<XAxisComponent>();
                    if (axixs.Count == 0) {
                        logger.Warn("Please Initialize XAxis First!");
                    }
                    else {
                        xAxis = axixs[0];
                    }
                }
                return xAxis;
            }
        }

        public virtual CartesianCoordinateAxis YAxis {
            get {
                if (yAxis == null) {
                    IList<YAxisComponent> axixs = WidgetsManager.GetCompatibleComponents<YAxisComponent>();
                    if (axixs.Count == 0) {
                        logger.Warn("Please Initialize YAxis First!");
                    }
                    else {
                        yAxis = axixs[0];
                    }
                }
                return yAxis;
            }
        }

        protected void CreateDependences() {
            IList<CartesianCoordinateComposite> composites = WidgetsManager.GetCompatibleComponents<CartesianCoordinateComposite>();
            if (composites == null || composites.Count <= 0) {
                IList<ChartComposite> chartCanvas = WidgetsManager.GetCompatibleComponents<ChartComposite>();
                if (chartCanvas == null || chartCanvas.Count <= 0) {
                    throw new InvalidOperationException("Not found ChartCompoiste component!");
                }
                chartCanvas[0].Children.Add(new CartesianCoordinateComposite());
            }
        }

        #region RelativeClientConsumer Members

        public virtual RelativeClientProvider GetClientProvider() {
            IList<CartesianCoordinateComposite> coordinates =
                WidgetsManager.GetCompatibleComponents<CartesianCoordinateComposite>();
            if (coordinates != null && coordinates.Count > 0) {
                return coordinates[0];
            }
            throw new InvalidOperationException("CartesianCoordinateComposite not found!");
        }

        #endregion
    }
}
